package com.newworld.dmsapp2.ui.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.newworld.dmsapp2.R;

public class PublicNoticeView extends LinearLayout {

	private static final String TAG = "滚动系统消息栏"; 
	private Context mContext;
	private ViewFlipper viewFlipper;
	private View scrollTitleView;
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				Log.e(TAG, "mHandler>>>");
				bindNotices();
				break;

			case -1:
				break;
			}
		}
	};

	/**
	 * 构造
	 * @param context
	 */
	public PublicNoticeView(Context context) {
		super(context);
		mContext = context;
		init();	
	}
	

	public PublicNoticeView(Context context,AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
		
	}
	
	/**
	 * 网络请求后返回公告内容进行适配
	 */
	@SuppressWarnings("deprecation")
	protected void bindNotices() {
		// TODO Auto-generated method stub
		Log.e(TAG, "内容适配");
		viewFlipper.removeAllViews();
		int i = 0;
		while(i<5){               
			String text = "系统公告:滚动消息"+i;
			TextView textView = new TextView(mContext);
			textView.setText(text);
			textView.setOnClickListener(new NoticeTitleOnClickListener(mContext,i+text));
			LayoutParams lp = new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			viewFlipper.addView(textView,lp);
			Log.e(TAG, "滚动内容"+i);
			i++;
		}
	}


	private void init(){
		Log.e(TAG, "init");
		bindLinearLayout();
		Message msg = new Message();
		msg.what = 1;
		Log.e(TAG, "mHandler>>>msg="+msg.what);
		mHandler.sendMessageDelayed(msg, 100);
		
	}

	/**
	 * 初始化自定义的布局
	 */
	@SuppressWarnings("deprecation")
	public void bindLinearLayout() {
		scrollTitleView = LayoutInflater.from(mContext).inflate(
				R.layout.main_public_notice_title, null);
		LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		addView(scrollTitleView, layoutParams);

		viewFlipper = (ViewFlipper) scrollTitleView
				.findViewById(R.id.flipper_scrollTitle);
		viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left));
		viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_out_right));
		viewFlipper.startFlipping();
		Log.e(TAG, "控件初始化");
	}
	
	
	/**
	 * 获取公告资讯
	 */
	public void getPublicNotices(){
		//网络请求获取
	}
	
	/**
	 * 公告title监听
	 * @author Nono
	 *
	 */
	class NoticeTitleOnClickListener implements OnClickListener{
		private Context context;
		private String titleid;
		
		public NoticeTitleOnClickListener(Context context, String whichText){
			this.context = context;
			this.titleid = whichText;
			Log.e(TAG, "绑定监听器");
		}
		public void onClick(View v) {
			// TODO Auto-generated method stub
			disPlayNoticeContent(context,titleid);
		}
		
	}

	/**
	 * 显示notice的具体内容
	 * @param context
	 * @param titleid
	 */
	public void disPlayNoticeContent(Context context, String titleid) {
		// TODO Auto-generated method stub
		Log.e(TAG, "点击跳转");
		Toast.makeText(context, titleid, Toast.LENGTH_SHORT).show();
		/*intent = new Intent(context, SecondActivity.class);
		intent.putExtra("tag", titleid);
		((Activity)context).startActivity(intent);*/
	}

}